﻿$(function () {
    $("#secImg").click(function () {
        ///注意后面的flag是必须的，如果不添加会导致部分浏览器不能刷新验证码
        $("#secImg").attr("src", "/Admin/Account/GetSecurityCode?flag=" + Math.random());
    });

    $("#username").focus();

    $('#login').validate({
        errorElement: 'span',
        errorClass: 'help-inline',
        focusInvalid: true,
        rules: {
            username: "required",
            password: "required",
            vercode: "required"
        },
        messages: {
            username: "请输入用户名",
            password: "请输入密码",
            vercode: "请输入验证码"
        },
        highlight: function (e) {
            $(e).closest('.form-group').removeClass('info').addClass('has-error');
        },
        success: function (e) {
            $(e).closest('.form-group').removeClass('has-error').addClass('info');
            $(e).remove();
        },
        submitHandler: function (form) {
            $.ajax({
                url: "/Admin/Account/Login",
                type: "POST",
                data: $('#login').serialize(),
                beforeSend: function () {
                    $('#btnLogin').text("登录中...");
                },
                success: function (response) {
                    if (response.success) {

                        layer.msg(response.msg, { icon: 1, time: 1000 }, function () {
                            window.top.location.href = response.url;
                        });
                        
                    } else {

                        layer.msg(response.msg, { icon: 2,time:1500 });

                    }
                },
                complete: function () {
                    $('#btnLogin').text("登录");
                }
            });
        }
    });
});
